package com.huiquan.framework.base;

import com.ibatis.sqlmap.client.SqlMapClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.SQLException;
import java.util.List;

/**
 * ClassName:BaseDao
 * Description:
 *
 * @author ChaoKai Wen email:chaokai.wen@witspring.com
 * @Date 14/11/25
 * @since Ver 1.0
 */
public abstract class BaseDao {

	public final Logger logger = LoggerFactory.getLogger(getClass());

	public int pageSize = 20;

	protected int delete(String id, Object o, SqlMapClient client) {
		try {
			return client.delete(id, o);
		} catch (SQLException e) {
			logger.error(e.getMessage());
			throw new RuntimeException();
		}
	}

	protected int delete(String id, SqlMapClient client) {
		try {
			return client.delete(id);
		} catch (SQLException e) {
			logger.error(e.getMessage());
			throw new RuntimeException();
		}
	}

	protected Object insert(String id, Object o, SqlMapClient client) {
		try {
			Object obj = client.insert(id, o);
			return obj;
		} catch (SQLException e) {
			logger.error(e.getMessage());
			throw new RuntimeException();
		}
	}

	protected Object insert(String id, SqlMapClient client) {
		try {
			Object obj = client.insert(id);
			return obj;
		} catch (SQLException e) {
			logger.error(e.getMessage());
			throw new RuntimeException();
		}
	}

	protected int update(String id, Object o, SqlMapClient client) {
		try {
			return client.update(id, o);
		} catch (Exception e) {
			logger.error(e.getMessage());
			throw new RuntimeException();
		}
	}

	protected int update(String id, SqlMapClient client) {
		try {
			return client.update(id);
		} catch (Exception e) {
			logger.error(e.getMessage());
			throw new RuntimeException();
		}
	}

	protected List<?> list(String id, Object o, SqlMapClient client) {
		try {
			return client.queryForList(id, o);
		} catch (SQLException e) {
			logger.error(e.getMessage());
			throw new RuntimeException();
		}
	}

	protected List<?> list(String id, SqlMapClient client) {
		try {
			return client.queryForList(id);
		} catch (SQLException e) {
			logger.error(e.getMessage());
			throw new RuntimeException();
		}
	}

	protected Object object(String id, SqlMapClient client) {
		try {
			return client.queryForObject(id);
		} catch (SQLException e) {
			logger.error(e.getMessage());
			throw new RuntimeException();
		}
	}

	protected Object object(String id, Object o, SqlMapClient client) {
		try {
			return client.queryForObject(id, o);
		} catch (SQLException e) {
			logger.error(e.getMessage());
			throw new RuntimeException();
		}
	}

	protected void startBatch(SqlMapClient client) {
		try {
			client.startBatch();
		} catch (SQLException e) {
			logger.error(e.getMessage());
			throw new RuntimeException();
		}
	}

	protected int executeBatch(SqlMapClient client) {
		try {
			return client.executeBatch();
		} catch (SQLException e) {
			logger.error(e.getMessage());
			throw new RuntimeException();
		}
	}

	protected void startTransaction(SqlMapClient client) {
		try {
			client.startTransaction();
		} catch (SQLException e) {
			logger.error(e.getMessage());
		}
	}

	protected void commitTransaction(SqlMapClient client) {
		try {
			client.commitTransaction();
		} catch (SQLException e) {
			logger.error(e.getMessage());
		}
	}

	protected void endTransaction(SqlMapClient client) {
		try {
			client.endTransaction();
		} catch (SQLException e) {
			logger.error(e.getMessage());
		}
	}

}
